Return null from GsonHelper array getters for a non-array property#4056
Conversation
…ray property These methods call gson JsonObject.getAsJsonArray(memberName), which casts the member to JsonArray and throws ClassCastException when the property exists but is not an array (e.g. a string or number). Pass the raw element to the null-safe getAsJsonArray(JsonElement) helper and make that helper treat a non-array element like null, so the methods return null for a non-array property just as they already do for a missing one. Adds regression tests.
|
@codex auggie review |
🤖 Augment PR SummarySummary: Makes GsonHelper’s array getters return Changes:
🤖 Was this summary useful? React with 👍 or 👎 |
Review 结论未发现需要阻塞合并的问题。该 PR 将 新增的回归用例覆盖了三个数组 getter 遇到非数组属性时返回 重点检查项
Testing
|
There was a problem hiding this comment.
Pull request overview
该 PR 修复了 GsonHelper 在读取数组属性时的健壮性问题:当 JSON 属性存在但类型不是数组时,不再抛出未检查的 ClassCastException,而是与“属性缺失/为 null”保持一致地返回 null。这属于 weixin-java-common 通用 JSON 工具层的容错增强,可降低上层反序列化在面对畸形输入时的崩溃风险。
Changes:
getIntArray/getStringArray/getLongArray改为先取原始JsonElement(o.get(name)),再走统一的getAsJsonArray(JsonElement)判定逻辑,避免 Gson 的强制类型转换导致的 CCE。getAsJsonArray(JsonElement)在元素非数组(含JsonNull、primitive、object 等)时返回null,与 “null element” 行为对齐。- 新增回归测试:覆盖三种数组 getter 在“属性存在但非数组”场景下应返回
null且不抛异常。
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java | 统一数组读取入口,避免非数组属性触发 ClassCastException,提升畸形输入容错性。 |
| weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.java | 增加三项回归测试,验证非数组属性时数组 getter 返回 null。 |
GsonHelper.getIntArray,getStringArrayandgetLongArraycall gson'sJsonObject.getAsJsonArray(memberName), which casts the member toJsonArray. If the property exists but is not a JSON array (e.g. a string or number), this throws an uncheckedClassCastExceptioninstead of returning gracefully.This passes the raw element via
o.get(string)to the existing null-safegetAsJsonArray(JsonElement)helper, and makes that helper treat a non-array element the same as null — so the three methods returnnullfor a non-array property, matching how they already handle a missing/null property. The other caller of the helper (WxOpenAuthorizationInfoGsonAdapter) is unaffected for valid arrays and becomes null-safe for malformed input.Adds regression tests for the three methods that fail before the change (CCE) and pass after.
AI assistance disclosure
This contribution was produced with the help of an AI pipeline. The pipeline processed a large amount of source code to surface suspected bugs, reproduced a subset of them with failing unit tests and generated candidate fixes, and prepared pull requests from the ones that held up. Each PR was then reviewed and verified by a human before being opened: the fix and test were checked by hand and the test was confirmed to fail before the change and pass after.